package cn.element.netty.future;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Slf4j
public class TestJdkFuture {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1.创建线程池
        ExecutorService pool = Executors.newFixedThreadPool(2);
        
        // 2.提交任务
        Future<Integer> future = pool.submit(() -> {
            log.debug("执行计算...");
            Thread.sleep(1000);
            return 50;
        });
        
        // 3.主线程通过future获取结果
        log.debug("等待结果...");
        Integer result = future.get();
        log.debug("结果是: {}", result);
    }

}
